package cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.matter;

import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.matter.MatterApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.GlideCircleTransform;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AuditMeetActivity extends BaseActivity {
    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.img_header)
    ImageView mImgHeader;
    @BindView(R.id.tv_person_name)
    TextView mTvPersonName;
    @BindView(R.id.tv_apply_res)
    TextView mTvApplyRes;
    @BindView(R.id.tv_meet_name)
    TextView mTvMeetName;
    @BindView(R.id.tv_meet_time)
    TextView mTvMeetTime;
    @BindView(R.id.tv_meet_addr)
    TextView mTvMeetAddr;
    @BindView(R.id.tv_meet_rom)
    TextView mTvMeetRom;
    @BindView(R.id.tv_meet_holder)
    TextView mTvMeetHolder;
    @BindView(R.id.tv_meet_recorder)
    TextView mTvMeetRecorder;
    @BindView(R.id.tv_meet_purpose)
    TextView mTvMeetPurpose;
    @BindView(R.id.textView4)
    TextView mTextView4;
    @BindView(R.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R.id.tv_end_time)
    TextView mTvEndTime;
    @BindView(R.id.tv_meet_content)
    TextView mTvMeetContent;
    @BindView(R.id.edit_reason)
    EditText mEditReason;
    @BindView(R.id.ra_agree)
    RadioButton mRaAgree;
    @BindView(R.id.ra_refuse)
    RadioButton mRaRefuse;

    MenuItem mMenuItem;

    MatterApi mMatterApi;

    MatterMeet mMatterNotice;

    @Override
    public int initContentView() {
        return R.layout.activity_audit_meet;
    }

    @Override
    public void initInjector() {
        mMatterApi = new MatterApi();
        mMatterNotice = getIntent().getParcelableExtra("meet");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());


        fillDetail(mMatterNotice);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item == mMenuItem) {
            submit();
        }

        return super.onOptionsItemSelected(item);
    }

    private void submit() {


        if (!mRaAgree.isChecked() && !mRaRefuse.isChecked()) {
            ToastUtil.showToast("尚未勾选审核");
            return;
        }


        if (TextUtils.isEmpty(mEditReason.getText())) {
            ToastUtil.showToast("审核理由不能为空");
            return;
        }

        mMatterApi.AuditMeet(String.valueOf(mMatterNotice.getId()), mRaAgree.isChecked() ? 1 : 3, mEditReason.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseResult>(mContext) {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        finish();
                    }
                });
    }






    private void fillDetail(MatterMeet entity) {
        mTvPersonName.setText(entity.getUsername());
//        mTvApplyTime.setText(entity.get());
        mTvMeetName.setText(entity.getName());
        mTvMeetTime.setText(entity.getStart());
        mTvMeetRom.setText(entity.getRoom_name());
        mTvMeetPurpose.setText(entity.getDetail());
        loadHead(Global.BASE_URL + entity.getAvatar(), mImgHeader);

    }

    private void loadHead(String path, ImageView imageView) {
        Glide.with(mContext)
                .load(path)
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mMenuItem = menu.add("确定");
        mMenuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }


}
